"""
 -*- coding: utf-8-*-
 @File: page_contact_list.py
 @Author: chen
 @time: 2023/09/10/ 18:59
"""
import time

import allure
from selenium.webdriver.common.by import By

from hogwartsHomework.homework_web_L4.dev.page_base import PageBase
from dev.page_login import LoginPage
from tools.record_log import logger


# 通讯录页面
class ContactListPage(PageBase):
    __BUTTON_HOME = (By.ID, "menu_index")
    __BUTTON_ADD = (By.XPATH, '//*[@class="ww_operationBar"]//*[text()="添加成员"]')

    def goto_home(self):
        """点击首页按钮，进入首页页面"""
        self.do_find(self.__BUTTON_HOME).click()
        from dev.page_home import HomePage
        return HomePage(self.driver)

    def goto_add_member(self):
        """进入添加成员页面"""
        # time.sleep(5)
        self.wait_until_clickable(self.__BUTTON_ADD)
        with allure.step(f"点击添加成员按钮：{self.__BUTTON_ADD}"):
            logger.info(f"点击添加成员按钮：{self.__BUTTON_ADD}")
            self.do_click(self.__BUTTON_ADD)
        from dev.page_add_member import AddMemberPage
        logger.info("成功进入添加成员页面")
        return AddMemberPage(self.driver)

    def delete_member(self, name):
        __checkbox_name = (By.XPATH, f'//*[text()="{name}"]/../..//*[@class="ww_checkbox"]')
        __btn_delete = """document.querySelector('.qui_btn.ww_btn.js_delete').click()"""
        __ensure_delete = (By.XPATH, '//*[@class="qui_btn ww_btn ww_btn_Blue"]')
        with allure.step(f"选择成员：{name}"):
            logger.info(f"选择成员：{name}")
            self.do_click(__checkbox_name)
        with allure.step(f"点击删除按钮"):
            logger.info(f"点击删除按钮")
            self.execute_js(__btn_delete)
        with allure.step("确认删除"):
            logger.info("确认删除")
            self.do_click(__ensure_delete)
        return ContactListPage(self.driver)

    def get_operation_result(self, operation):
        bubble_ele = (By.XPATH, f'//*[contains(text(),"{operation}")]')
        self.wait_until_visibility(bubble_ele)
        # 获取冒泡文本信息
        with allure.step(f"获取冒泡文本信息"):
            logger.info(f"获取冒泡文本信息")
            msg = self.do_find(bubble_ele).text
        logger.info(f'冒泡消息是：{msg}')
        # 返回文本信息
        return msg

    def get_all_member(self):
        __member = (By.CSS_SELECTOR, '.member_colRight_memberTable_td')
        # __member = (By.CSS_SELECTOR, '[data-id="1688857587459018"]')
        with allure.step("获取成员列表内容"):
            logger.info("获取成员列表内容")
            members = self.do_finds(__member)
        contents = []
        for i in members:
            # print(i.text)
            contents.append(i.text)
        logger.info(f"成员列表内容：{contents}")
        return contents


if __name__ == '__main__':
    cur_page = LoginPage().goto_login().login().goto_contact_list()
    # cur_page.get_all_member()
    cur_page.delete_member("钟离")
    print(cur_page.get_operation_result("删除"))
